package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.DruidJdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 高圆圆
 * @date 2022/12/27 17:04
 * 针对用户的数据访问接口实现
 */
public class UserDaoImpl  implements UserDao {
    /**
     * 数据访问接口通过用户名查询用户
     * @param username 用户名昵称
     * @return 返回用户实体
     */
    @Override
    public User selectUserByUsername(String username) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where username = ?" ;
        User user = qr.query(sql, new BeanHandler<User>(User.class), username);
        return user;
    }

    /**
     * 数据访问接口添加用户
     * @param user  用户实体对象
     * @return 返回影响的行数
     */
    @Override
    public int insertUser(User user) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)" ;
        //更新
        int count = qr.update(sql,
                user.getUid(),
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex(),
                user.getState(),
                user.getCode());
        return count    ;
    }

    /**
     * 数据访问接口通过激活码查询指定用户
     * @param code 用户的激活码
     * @return 返回值查询到指定的用户
     */
    @Override
    public User selectUserByCode(String code) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where code =?" ;
        //查询
        User user = qr.query(sql, new BeanHandler<User>(User.class), code);
        return user;
    }

    /**
     * 数据访问接口修改用户的激活状态
     * @param user  指定的用户的
     * @return 返回影响行数
     */
    @Override
    public int updateUserState(User user) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "update user set state = ? where uid = ?" ;
        int count = qr.update(sql,
                user.getState(),
                user.getUid());
        return count;
    }

    /**
     * 通过用户名查询指定用户
     * @param username 用户名
     * @return 返回指定的用户实体
     */
    @Override
    public User selectUserByUserName(String username) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where username = ?" ;
        //查询
        User user = qr.query(sql, new BeanHandler<User>(User.class), username);
        return user;
    }

}
